<?php

namespace App\Admin\Controllers\Shop;

use App\Models\Shop\GoodsClass;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class GoodsClassController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\Shop\GoodsClass';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new GoodsClass());

        $grid->column('id', __('field.id'));
        $grid->column('icon', __('field.icon'));
        $grid->column('parent_id', __('field.parent_id'));
        $grid->column('name', __('field.name'));
        $grid->column('sort', __('field.sort'));
        $grid->column('created_at', __('field.created_at'));
        $grid->column('updated_at', __('field.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(GoodsClass::findOrFail($id));

        $show->field('id', __('field.id'));
        $show->field('icon', __('field.icon'));
        $show->field('parent_id', __('field.parent_id'));
        $show->field('name', __('field.name'));
        $show->field('sort', __('field.sort'));
        $show->field('created_at', __('field.created_at'));
        $show->field('updated_at', __('field.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new GoodsClass());
        $key = 0;
        if ($form->isEditing()) {
            $key = getAdminEditId();
        }
        $form->text('icon', __('field.icon'));
        $form->select('parent_id', __('field.parent_id'))
            ->options(
                (new GoodsClass())->get(['id','name'])
                    ->where('id','<>',$key)
                    ->pluck('name','id')
                );
        $form->text('name', __('field.name'));
        $form->number('sort', __('field.sort'))->default(99);

        return $form;
    }
}
